//-----------------------------------------------------------------------
//<copyright file="KhoHangDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class KhoHangDAO : SQLDatabase, IKhoHangDAO
	{

		#region Create KhoHang methods

		public KhoHang CreateKhoHang(KhoHang khoHang)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhoHangInsert");
				AddOutParameter(command, "@MaKho", SqlDbType.NVarChar, khoHang.MaKho);
				AddInParameter(command, "@TenKho", SqlDbType.NVarChar, khoHang.TenKho);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, khoHang.DiaChi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, khoHang.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, khoHang.SoFax);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, khoHang.Email);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, khoHang.GhiChu);

				ExecuteNonQuery(command);
                khoHang.MaKho = (string)GetParameterValue(command, "@MaKho");
				return khoHang;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateKhoHangException", ex);
			}
		}

		#endregion

		#region Update KhoHang methods

		public void UpdateKhoHang(KhoHang khoHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhoHangUpdate");

				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, khoHang.MaKho);
				AddInParameter(command, "@TenKho", SqlDbType.NVarChar, khoHang.TenKho);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, khoHang.DiaChi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, khoHang.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, khoHang.SoFax);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, khoHang.Email);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, khoHang.GhiChu);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateKhoHangException", ex);
			}
		}

		#endregion

		#region Delete KhoHang methods

		public void DeleteKhoHang(KhoHang khoHang)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhoHangDelete");

				AddInParameter(command, "@MaKho", SqlDbType.NVarChar, khoHang.MaKho);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteKhoHangException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll KhoHang methods

		public KhoHangs GetAllKhoHang()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhoHangGetList");
				KhoHangs Result = new KhoHangs();
				List<KhoHang> List = ExecuteQuery<KhoHang>(command,true);
				foreach(KhoHang item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllKhoHangException", ex);
			}
		}

		#endregion


		#region GetKhoHang methods

		public KhoHang GetKhoHangByID(string maKho)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spKhoHangGet");

				AddInParameter(command, "@maKho", SqlDbType.NVarChar, maKho);

				return ExecuteQuery<KhoHang>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetKhoHangBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
